package org.csu.mypetstoreback.controller;

import com.alibaba.fastjson.JSON;
import org.csu.mypetstoreback.domain.Category;
import org.csu.mypetstoreback.domain.Item;
import org.csu.mypetstoreback.domain.Product;
import org.csu.mypetstoreback.service.CatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.ui.Model;
import java.util.List;
import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;


@Controller
@RequestMapping(produces = "application/json; charset=utf-8")
@SessionAttributes({"catalog","userList","orderList"})
public class CatalogController {

    private static final List<String> LANGUAGE_LIST;
    private static final List<String> CATEGORY_LIST;
    private static final List<String> CARD_LIST;

    static {
        List<String> langList = new ArrayList<String>();
        langList.add("ENGLISH");
        langList.add("CHINESE");
        LANGUAGE_LIST = Collections.unmodifiableList(langList);

        List<String> catList = new ArrayList<String>();
        catList.add("FISH");
        catList.add("DOGS");
        catList.add("REPTILES");
        catList.add("CATS");
        catList.add("BIRDS");

        CATEGORY_LIST = Collections.unmodifiableList(catList);

        List<String> cardList = new ArrayList<String>();
        cardList.add("Visa");
        cardList.add("MasterCard");
        cardList.add("American Express");
        CARD_LIST = Collections.unmodifiableList(cardList);
    }
    @Autowired
    private CatalogService catalogService;
    @Autowired
    private CatalogService productService;

    @Autowired
    private CatalogService updateproductService;

  

    @GetMapping("/catalog/main")
    public  String viewmain(){
        
        
        //logger.info("进入主界面"); // 输出DEBUG级别的日志
        return  "catalog/main";
    }



    @GetMapping("/catalog/item")
    public String product(Model model){
        System.out.println("getProducts");
        List<Product> productList=productService.getProducts();
        // List<Product> productList=productService.getProducts();
        System.out.println(productList.get(0));
        model.addAttribute("productList",productList);
        model.addAttribute("updateProduct",new Product());
        model.addAttribute("updateItem",new Item());
        model.addAttribute("CARD_LIST",CARD_LIST);
        return "catalog/item";
    }



    @GetMapping("/getitems")
    public void getItems(String productId,HttpServletResponse response) throws IOException {
        List<Item> items = catalogService.getItemListByProduct(productId);
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.write(JSON.toJSONString(items));
        out.flush();
        out.close();
        //      model.addAttribute("items",items);
    }


    @PostMapping("/addcategory")
    public String addCategory(HttpServletRequest request){
        System.out.println("addcatrgroy");
        String categoryId = request.getParameter("categoryId");
        String categoryName = request.getParameter("categoryName");
        String categoryDescn = request.getParameter("categoryDescn");
        catalogService.addCategory(categoryId,categoryName,categoryDescn);
        return "redirect:/catalog/item";
    }

    @GetMapping("/getcategory")
    public void getCategory(HttpServletResponse response) throws IOException {
        List<Category> categories = catalogService.getCategoryList();
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.write(JSON.toJSONString(categories));
        out.flush();
        out.close();
    }
  
    @GetMapping("/getProducts")
    public void getProducts(String keyword,HttpServletResponse response) throws IOException {
        System.out.println(keyword);
        List<Product> products;
        if(keyword==null) {
            products = catalogService.getProducts();
        }
        else {
            products = catalogService.searchProductList(keyword);
        }

        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.write(JSON.toJSONString(products));
        out.flush();
        out.close();
    }

    @PostMapping("/addproduct")
    public String addProduct(HttpServletRequest request){
        System.out.println("aproduct");

        String productId = request.getParameter("productId");
        String categoryId = request.getParameter("categoryId");
        String name = request.getParameter("name");
        String description = request.getParameter("description");
        System.out.println(productId+categoryId+name+description);
        Product newProduct = new Product();
        newProduct.setCategoryId(categoryId);
        newProduct.setProductId(productId);
        newProduct.setName(name);
        newProduct.setDescription(description);
        catalogService.addProduct(productId,categoryId, name, description);
        return "redirect:/catalog/item";
    }

    @PostMapping("/additem")
    public String addItem(HttpServletRequest request){
        System.out.println("additem");
        String productId=request.getParameter("productId");
        String itemId = request.getParameter("itemId");
        String unitcost = request.getParameter("unitcost");
        String listprice = request.getParameter("listprice");
        String supplier = request.getParameter("supplier");
        String status = request.getParameter("status");
        String attr = request.getParameter("attr");
        String quantity = request.getParameter("quantity");
        Item item = new Item();

        item.setProductId(productId);
        item.setItemId(itemId);
        item.setListPrice(new BigDecimal(listprice));
        item.setUnitCost(new BigDecimal(unitcost));
        item.setSupplierId(Integer.parseInt(supplier));
        item.setStatus(status);
        item.setAttribute1(attr);
        item.setQuantity(Integer.parseInt(quantity));
        catalogService.addItem(item);
        return "redirect:/catalog/item";
    }
    @PostMapping("/updateproduct")
    public String updateProduct(Product updateProduct){

        List<Product> productList=productService.getProducts();
        System.out.println(productList.get(0));
        productService.updateProduct(updateProduct);
        // List<Product> productList=productService.getProducts();
        // model.addAttribute("productList",productList);
        List<Product> productLis=productService.getProducts();
        System.out.println(productLis.get(0));
        List<Product> productLi=productService.getProducts();
        System.out.println(productLi.get(0));
        List<Product> productL=productService.getProducts();
        System.out.println(productL.get(0));
        List<Product> product=productService.getProducts();
        System.out.println(product.get(0));

        return "redirect:/catalog/item";

    }


    @PostMapping("/updateitem")
    public String updateItem(Item updateItem){

        catalogService.updateItem(updateItem);

        return "redirect:/catalog/item";

    }

    @GetMapping("/getproducts")
    public void getproducts(HttpServletResponse response) throws IOException {
        List<Product> products = catalogService.getProducts();
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.write(JSON.toJSONString(products));
        out.flush();
        out.close();
    }
    @GetMapping("/getProduct")
    public void getProduct(String productId, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=UTF-8");
        Product product=catalogService.getProduct(productId);
        PrintWriter out = response.getWriter();
        out.write(JSON.toJSONString(product));
        out.flush();
        out.close();
    }
    
    @GetMapping("/getitem")
    public void getItem(String itemId, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=UTF-8");
        Item item=catalogService.getItem(itemId);
        PrintWriter out = response.getWriter();
        out.write(JSON.toJSONString(item));
        out.flush();
        out.close();
    }

    @GetMapping("/deleteproduct")
    public void deleteProduct(String productId, HttpServletResponse response) throws IOException {
        catalogService.deleteItemByProduct(productId);
        catalogService.deleteProduct(productId);

        PrintWriter out = response.getWriter();
        out.print("success");
        out.flush();
        out.close();

    }
    @GetMapping("/deleteitem")
    public void deleteItem(String itemId, HttpServletResponse response) throws IOException {

        catalogService.deleteItem(itemId);
        PrintWriter out = response.getWriter();
        out.print("success");
        out.flush();
        out.close();

    }
}
